$(function () {
    let showPageFlag = 0;
    let tabelList = [1, 2, 3, 4, 5];
    // 当前页码
    let currentPage = 1;
    // 页面尺寸
    let pageSize = 19;
    //总页数
    let totalPageNum = 0;
    let windowH = $(window).height() - 35;
    $("#wrapper").attr('style','height:' + windowH + 'px');
    let currentFileId = "";
    let count = 0;
    $("#submitFile").click(function () {
        submitFile();
    });
    function submitFile() {
        let file = $("#file")[0].files[0];
        let formData = new FormData();
        formData.append('file',file)
        $(".load").attr('style','display:block;')
        $.ajax({
            type: "POST",
            cache:false,
            processData: false,
            contentType: false,
            dataType: "json",
            url: "/file/upload",
            data: formData,
            success: function (result) {
                $(".load").attr('style','display:none;');
                if (result.returnCode == "0000") {
                    currentFileId = result.fileNameId;
                    $(".sure").attr('style','display:block;');
                    $(".rollback").attr('style','display:block;');
                    alert(result.returnMsg + '请确认上传内容是否正确。若内容正确，点击右侧确认。若内容有误，点击右侧回滚。');
                }
                if (result.returnCode == "9999") {
                    alert(result.returnMsg)
                };
                // alert(result.returnMsg)
            },
            error : function() {
                $(".load").attr('style','display:none;');
                alert("异常！请检查上传表格格式是否正确！");
            }
        });
    }

    $("#toSearch").click(function () {
        currentPage = 1;
        toSearch();
    })
    function toSearch() {
        $("#table").html("");
        $(".total").html("共n条");
        $(".current").html("第n页");
        $("#page-nums").html("");
        $(".load").attr('style','display:block;')
        let str = '<tr>\n' +
            '                       <td>年份</td>\n' +
            '                       <td>批次</td>\n' +
            '                       <td>院校名称</td>\n' +
            '                       <td>科类</td>\n' +
            '                       <td>专业</td>\n' +
            '                       <td>计划招生</td>\n' +
            '                       <td>投档最低分</td>\n' +
            '                       <td>最低分位次</td>\n' +
            '                   </tr>';
        $("#table").append(str);
        let beginYear = $(".beginYear").val();
        let endYear = $(".endYear").val();
        let schoolName = $(".schoolName").val();
        let branch = $(".branch").val();
        let major = $(".major").val();
        let zdfwc = $(".zdfwc").val();
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/searchInfo",
            data: {
                beginYear: beginYear,
                endYear: endYear,
                schoolName: schoolName,
                branch: branch,
                major: major,
                zdfwc: zdfwc,
                pageSize: pageSize,
                pageIndex: currentPage
            },
            success: function (result) {
                $(".load").attr('style','display:none;');
                if (result.returnCode == "0000"){
                    let beans = result.beans;
                    let total = result.total;
                    let current = result.current;
                    let pages = result.pages;
                    totalPageNum = pages;
                    if (current == pages){
                        $('.nextPage').attr('disabled','disabled');
                    }else {
                        $('.nextPage').removeAttr('disabled');
                    }
                    if (current == 1){
                        $('.beforePage').attr('disabled','disabled');
                    }else {
                        $('.beforePage').removeAttr('disabled');
                    }
                    $(".total").html("共" + total + "条");
                    $(".current").html("第" + current + "页");
                    let str = "";
                    let color = "";
                    let beforeColor = 100;
                    beans.forEach(function (bean,index) {
                        let year = bean.year;
                        let batch = bean.batch;
                        let batchCode = bean.batchCode;
                        let schoolName = bean.schoolName;
                        let schoolCode = bean.schoolCode;
                        let branch = bean.branch;
                        let branchCode = bean.branchCode;
                        let major = bean.major;
                        let planNum = bean.planNum;
                        let lowestScore = bean.lowestScore;
                        let zdfwc = bean.zdfwc;
                        let randomNum = parseInt(Math.random() * 6);
                        if (beforeColor == randomNum){
                            randomNum = parseInt(Math.random() * 6);
                        }
                        if (beforeColor == randomNum){
                            randomNum = parseInt(Math.random() * 6);
                        }
                        if (beforeColor == randomNum){
                            randomNum = parseInt(Math.random() * 6);
                        }
                        beforeColor = randomNum;
                        if (randomNum == 0){
                            color = '#FBEFEF';
                        }else if (randomNum == 1){
                            color = '#FBFBEF';
                        }else if (randomNum == 2){
                            color = '#F8E0F7';
                        }else if (randomNum == 3){
                            color = '#EFFBFB';
                        }else if (randomNum == 4){
                            color = '#CEF6EC';
                        }else {
                            color = '';
                        }
                        str = '<tr batchCode="' + batchCode + '"schoolCode="' + schoolCode + '" branchCode="' + branchCode + '" randomNum="' + randomNum + '" style="height: 30px; background-color: ' + color + '">' +
                            '<td>' + year + '</td>' +
                            '<td>' + batch + '</td>' +
                            '<td>' + schoolName + '</td>' +
                            '<td>' + branch + '</td>' +
                            '<td>' + major + '</td>' +
                            '<td>' + planNum + '</td>' +
                            '<td>' + lowestScore + '</td>' +
                            '<td>' + zdfwc + '</td>' +
                            '<td style="display: none;">' + batchCode + '</td>' +
                            '<td style="display: none;">' + schoolCode + '</td>' +
                            '<td style="display: none;">' + branchCode + '</td>' +
                            '</tr>';
                        count ++;
                        $("#table").append(str);
                    });
                    if (totalPageNum <= 5){
                        let pageStr = '';
                        for (let g = 1; g <= totalPageNum; g ++){
                            pageStr = pageStr + "<button class='page-num'>" + g + "</button>";
                        }
                        $("#page-nums").append(pageStr);
                    }else {
                        let pageStr = '';
                        for (let g = 0; g < tabelList.length; g ++){
                            pageStr = pageStr + "<button class='page-num'>" + tabelList[g] + "</button>";
                        }
                        pageStr = pageStr + " ... ";
                        for (let f = totalPageNum - 4; f <= totalPageNum ; f ++){
                            pageStr = pageStr + "<button class='page-num'>" + f + "</button>";
                        }
                        $("#page-nums").append(pageStr);
                    }
                    let qwStr = "前往" + "<input type='text' value='" + currentPage + "' class='inputPage' style='width: 30px;'>" + "页" + "<button class='goInputPage'>go</button>";
                    $("#page-nums").append(qwStr);
                    showPageFlag = 1;
                    $('.goInputPage').click(function () {
                        let inputPageNum = $('.inputPage').val();
                        currentPage = parseInt(inputPageNum);
                        toSearch();
                    });
                    $('.inputPage').keypress(function (event) {
                        if (event.which === 13){
                            let inputPageNum = $('.inputPage').val();
                            currentPage = parseInt(inputPageNum);
                            toSearch();
                        }
                    });
                }else {
                    alert(result.returnMsg);
                }
            },
            error : function() {
                alert("异常！");
                $(".load").attr('style','display:none;');
            }
        });
    }
    //上一页
    $('.beforePage').click(function () {
        currentPage = parseInt(currentPage) - 1;
        console.log(currentPage)
        toSearch();
    })
    //下一页
    $('.nextPage').click(function () {
        currentPage = parseInt(currentPage) + 1;
        console.log(currentPage)
        toSearch();
    })
    $(document).on('click','.page-num',function (item) {
        let toPage = parseInt($(this).html());
        currentPage = toPage;
        console.log(currentPage)
        toSearch();
    });

    //导出excel
    $("#outputExcel").click(function () {
        download();
    });
    function download(){
        let beginYear = $(".beginYear").val();
        if (beginYear == ""){
            beginYear = "aa"
        }
        let endYear = $(".endYear").val();
        if (endYear == ""){
            endYear = "aa"
        }
        let schoolName = $(".schoolName").val();
        if (schoolName == ""){
            schoolName = "aa"
        }
        let branch = $(".branch").val();
        if (branch == ""){
            branch = "aa"
        }
        let major = $(".major").val();
        if (major == ""){
            major = "aa"
        }
        let zdfwc = $(".zdfwc").val();
        if (zdfwc == ""){
            zdfwc = "aa"
        }
        let data = {
            beginYear: beginYear,
            endYear: endYear,
            schoolName: schoolName,
            branch: branch,
            major: major,
            zdfwc:zdfwc
        }
        let jsonData = JSON.stringify(data);
        let s = beginYear + "-" + endYear + "-" + schoolName + "-" + branch + "-" + major + "-" + zdfwc;
        window.location.href="/file/outputExcel1?queryCondition=" + s;
    }

    $("#sureDeleteBean").click(function () {
        let action = "sure";
        deleteBean(action);
    });
    $("#rellbackDeleteBean").click(function () {
        deleteBean("rollback");
    });
    $("#deleteDatabase").click(function () {
        deleteBean("deleteDatabase");
    });
    function deleteBean(action) {
        let currentFileNameId = "";
        if(action == "deleteDatabase"){
            let isdo = confirm("执行此操作会将包括之前上传的所有记录全部清除，请谨慎操作！！是否继续执行")
            if(!isdo){
                return;
            }
        }
        if (action == "rollback"){
            currentFileNameId = currentFileId;
        }
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/searchInfo/deleteBean",
            data: {
                action: action,
                fileNameId: currentFileNameId
            },
            success: function (result) {
                if (result.returnCode == "0000"){
                    let action = result.action;
                    if (action == 'sure'){
                        $(".rollback").attr('style','display:none;');
                        $(".sure").attr('style','display:none;');
                    }
                    if (action == 'rollback'){
                        $(".rollback").attr('style','display:none;');
                        $(".sure").attr('style','display:none;');
                    }
                    alert(result.returnMsg);
                }else {
                    alert(result.returnMsg);
                }
            },
            error : function() {
                alert("异常！");
            }
        });
    }
})